package com.bjc.platform.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bjc.platform.model.CustomUserDetails;
import com.bjc.platform.service.AdmMenuService;
import com.bjc.platform.util.StaticMethod;
import com.bjc.platform.vo.ExtTreeModel;
import com.bjc.platform.vo.FunctionItem;

@Controller
@RequestMapping("/menu")
public class AdmMenuController extends BaseController {
	@Autowired
	private AdmMenuService admMenuService;

	@RequestMapping(value = "/getTree", method = RequestMethod.GET)
	@ResponseBody
	public Object getTree(@RequestParam HashMap<String, Object> reqParam) {
		String roleId = StaticMethod.object2String(reqParam.get("id"));
		List<Map<String, Object>> auths = admMenuService.getMenuTree(roleId);

		try {
			ExtTreeModel root = ExtTreeModel.assamblyTreeStructForExt(auths,
					true, true, false);
			root.setText("权限列表");
			root.setExpanded(true);
			return root;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@RequestMapping(value = "/getMenuByUserId", method = RequestMethod.GET)
	@ResponseBody
	public Object getMenuByUserId(@RequestParam HashMap<String, Object> reqParam) {
		CustomUserDetails user = StaticMethod.getUser();
		List<Map<String, Object>> auths = admMenuService
				.getMenuTreeByUserId(user.getId());

		List<Object[]> menu = buildMenu(auths);

		return menu;
	}

	private List<Object[]> buildMenu(List<Map<String, Object>> auths) {

		List<Object[]> menus = new ArrayList<Object[]>();
		String icon = null;
		String code = null;
		String id = null;
		String pid = null;
		String name = null;
		Object[] menu = null;
		List<FunctionItem> items = null;
		FunctionItem item = null;
		if (auths != null && auths.size() > 0) {
			for (Map<String, Object> a : auths) {
				icon = a.get(ExtTreeModel.ICON) == null ? "" : String.valueOf(a
						.get(ExtTreeModel.ICON));
				code = a.get(ExtTreeModel.CODE) == null ? "" : String.valueOf(a
						.get(ExtTreeModel.CODE));
				id = a.get(ExtTreeModel.ID) == null ? "" : String.valueOf(a
						.get(ExtTreeModel.ID));
				pid = a.get(ExtTreeModel.PARENT_ID) == null ? "" : String
						.valueOf(a.get(ExtTreeModel.PARENT_ID));
				name = a.get(ExtTreeModel.NAME) == null ? "" : String.valueOf(a
						.get(ExtTreeModel.NAME));

				item = new FunctionItem();
				item.setId(id);
				item.setName(name);
				item.setResource(code); // code 对应Ext组件的别名
				item.setIcon(icon);

				if (pid == null || StringUtils.isEmpty(String.valueOf(pid))) {
					items = new ArrayList<FunctionItem>();
					menu = new Object[2];
					menu[0] = item;
					menu[1] = items;
					menus.add(menu);
				} else {
					items.add(item);
				}
			}
		}

		return menus;
	}
}
